package com.doubi.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.doubi.constants.SystemConstants;
import com.doubi.mapper.SysDeptMapper;
import com.doubi.model.sys.TSysDept;
import com.doubi.model.sys.TSysUser;
import com.doubi.utils.DataUtil;
import com.doubi.vo.PageVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class SysDeptService extends ServiceImpl<SysDeptMapper,TSysDept> {

    @Autowired
    private SysDeptMapper sysDeptMapper;

    /**
     * 功能描述：获取部门分页数据
     * @author   MrDoubi 2019-06-27 17:03
     * @param page          Page对象
     * @param sysDept       TSysDept对象
     * @return              返回Page对象
     */
    public PageVo<TSysDept> queryPage(PageVo<TSysDept> page, TSysDept sysDept) {
        QueryWrapper<TSysDept> wrapper = new QueryWrapper<>();
        if(DataUtil.isNotEmpty(sysDept.getDeptName())){
            wrapper.like("dept_name",sysDept.getDeptName());
        }
        wrapper.orderByAsc("sort_num");
        sysDeptMapper.selectPage(page,wrapper);
        return page;
    }

    /**
     * 功能描述：获取所有的部门
     * @author   MrDoubi 2019-06-27 17:41
     * @return              返回<TSysDept>对象
     */
    public List<TSysDept> getAllBySortNum() {
        QueryWrapper<TSysDept> wrapper = new QueryWrapper<>();
        wrapper.eq("status",SystemConstants.STATUS.STATUS_1);
        wrapper.orderByAsc("sort_num");
        return sysDeptMapper.selectList(wrapper);
    }

    /**
     * 功能描述：启用禁用部门
     * @author   MrDoubi 2019-06-28 11:04
     * @param oid           部门oid
     * @param sysUser       TSysUser对象
     * @return              返回int型结果
     *
     */
    public Integer updateStatus(String oid, TSysUser sysUser) {
        TSysDept sysDept = super.getById(oid);
        sysDept.setUpdateBy(sysUser.getOid());
        sysDept.setUpdateTime(new Date());
        if (SystemConstants.STATUS.STATUS_1.equals(sysDept.getStatus())) {
            sysDept.setStatus(SystemConstants.STATUS.STATUS_0);
            sysDeptMapper.updateXiaJiStatus(oid, SystemConstants.STATUS.STATUS_0, sysUser.getOid());
            sysDeptMapper.updateById(sysDept);
        } else {
            sysDept.setStatus(SystemConstants.STATUS.STATUS_1);
            sysDeptMapper.updateById(sysDept);
        }
        return 1;
    }



    /**
     * 功能描述：根据oid获取下级部门
     * @author   MrDoubi 2019-07-02 15:11
     * @param oid           部门oid
     * @return              返回List<TSysDept>对象
     */
    public List<TSysDept> getLowDeptListByOid(String oid) {
        QueryWrapper<TSysDept> wrapper = new QueryWrapper<>();
        wrapper.like("ancestors",oid);
        return sysDeptMapper.selectList(wrapper);
    }

    /**
     * 功能描述：伪删除操作
     * @author   MrDoubi 2019-07-02 15:11
     * @param oid           部门oid
     */
    public void updateByDel(String oid,String userOid){
        TSysDept sysDept = super.getById(oid);
        sysDept.setIsDelete(SystemConstants.DELETE_STATUS.STATUS_0);
        sysDept.setUpdateBy(userOid);
        sysDept.setUpdateTime(new Date());
        sysDeptMapper.updateById(sysDept);
        sysDeptMapper.deleteById(sysDept.getOid());
    }
}
